home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / K0ET9B (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  8.3 KB  |  402 lines

  1. package sun.awt.image;
  2.  
  3. import java.awt.image.ImageConsumer;
  4. import java.awt.image.ImageProducer;
  5. import java.io.BufferedInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8.  
  9. public abstract class InputStreamImageSource implements ImageProducer, ImageFetchable {
  10.    PixelStore pixelstore;
  11.    ImageConsumerQueue consumers;
  12.    ImageDecoder decoder;
  13.    ImageDecoder decoders;
  14.    boolean awaitingFetch = false;
  15.  
  16.    public void addConsumer(ImageConsumer ic) {
  17.       this.addConsumer(ic, false);
  18.    }
  19.  
  20.    synchronized void addConsumer(ImageConsumer ic, boolean produce) {
  21.       this.checkSecurity((Object)null, false);
  22.  
  23.       for(ImageDecoder id = this.decoders; id != null; id = id.next) {
  24.          if (id.isConsumer(ic)) {
  25.             return;
  26.          }
  27.       }
  28.  
  29.       ImageConsumerQueue cq;
  30.       for(cq = this.consumers; cq != null && cq.consumer != ic; cq = cq.next) {
  31.       }
  32.  
  33.       if (cq == null) {
  34.          cq = new ImageConsumerQueue(this, ic);
  35.          cq.next = this.consumers;
  36.          this.consumers = cq;
  37.       } else {
  38.          if (!cq.secure) {
  39.             Object context = null;
  40.             SecurityManager security = System.getSecurityManager();
  41.             if (security != null) {
  42.                context = security.getSecurityContext();
  43.             }
  44.  
  45.             if (cq.securityContext == null) {
  46.                cq.securityContext = context;
  47.             } else if (cq.securityContext != context) {
  48.                this.errorConsumer(cq);
  49.                throw new SecurityException("Applets are trading image data!");
  50.             }
  51.          }
  52.  
  53.          cq.interested = true;
  54.       }
  55.  
  56.       if (produce && this.decoder == null) {
  57.          this.startProduction();
  58.       }
  59.  
  60.    }
  61.  
  62.    private void badDecoder() {
  63.       synchronized(this){}
  64.  
  65.       ImageConsumerQueue cq;
  66.       try {
  67.          cq = this.consumers;
  68.          this.consumers = null;
  69.          this.awaitingFetch = false;
  70.       } catch (Throwable var4) {
  71.          throw var4;
  72.       }
  73.  
  74.       this.errorAllConsumers(cq);
  75.    }
  76.  
  77.    abstract boolean checkSecurity(Object var1, boolean var2);
  78.  
  79.    synchronized int countConsumers() {
  80.       ImageDecoder id = this.decoders;
  81.  
  82.       int i;
  83.       for(i = this.countConsumers(this.consumers); id != null; id = id.next) {
  84.          i += this.countConsumers(id.queue);
  85.       }
  86.  
  87.       return i;
  88.    }
  89.  
  90.    int countConsumers(ImageConsumerQueue cq) {
  91.       int i;
  92.       for(i = 0; cq != null; cq = cq.next) {
  93.          ++i;
  94.       }
  95.  
  96.       return i;
  97.    }
  98.  
  99.    protected ImageDecoder decoderForType(InputStream is, String content_type) {
  100.       return null;
  101.    }
  102.  
  103.    public void doFetch() {
  104.       do {
  105.          synchronized(this) {
  106.             if (this.consumers == null) {
  107.                this.awaitingFetch = false;
  108.                return;
  109.             }
  110.          }
  111.       } while(this.updateFromStore());
  112.  
  113.       ImageDecoder imgd = this.getDecoder();
  114.       if (imgd == null) {
  115.          this.badDecoder();
  116.       } else {
  117.          this.setDecoder(imgd);
  118.  
  119.          try {
  120.             imgd.produceImage();
  121.          } catch (IOException var8) {
  122.             ((Throwable)var8).printStackTrace();
  123.          } catch (ImageFormatException var9) {
  124.             ((Throwable)var9).printStackTrace();
  125.          } finally {
  126.             this.removeDecoder(imgd);
  127.             this.errorAllConsumers(imgd.queue);
  128.          }
  129.       }
  130.  
  131.    }
  132.  
  133.    synchronized void doneDecoding(ImageDecoder mydecoder) {
  134.       if (this.decoder == mydecoder) {
  135.          this.decoder = null;
  136.          if (this.consumers != null) {
  137.             this.startProduction();
  138.          }
  139.       }
  140.  
  141.    }
  142.  
  143.    private void errorAllConsumers(ImageConsumerQueue cq) {
  144.       for(; cq != null; cq = cq.next) {
  145.          if (cq.interested) {
  146.             this.errorConsumer(cq);
  147.          }
  148.       }
  149.  
  150.    }
  151.  
  152.    private void errorConsumer(ImageConsumerQueue cq) {
  153.       cq.consumer.imageComplete(1);
  154.       this.removeConsumer(cq.consumer);
  155.    }
  156.  
  157.    synchronized void flush() {
  158.       this.pixelstore = null;
  159.       this.decoder = null;
  160.    }
  161.  
  162.    protected abstract ImageDecoder getDecoder();
  163.  
  164.    protected ImageDecoder getDecoder(InputStream is) {
  165.       if (!is.markSupported()) {
  166.          is = new BufferedInputStream(is);
  167.       }
  168.  
  169.       try {
  170.          is.mark(6);
  171.          int c1 = is.read();
  172.          int c2 = is.read();
  173.          int c3 = is.read();
  174.          int c4 = is.read();
  175.          is.read();
  176.          is.read();
  177.          is.reset();
  178.          is.mark(-1);
  179.          if (c1 == 71 && c2 == 73 && c3 == 70 && c4 == 56) {
  180.             return new GifImageDecoder(this, is);
  181.          }
  182.  
  183.          if (c1 == 255 && c2 == 216 && c3 == 255) {
  184.             return new JPEGImageDecoder(this, is);
  185.          }
  186.  
  187.          if (c1 == 35 && c2 == 100 && c3 == 101 && c4 == 102) {
  188.             return new XbmImageDecoder(this, is);
  189.          }
  190.       } catch (IOException var6) {
  191.       }
  192.  
  193.       return null;
  194.    }
  195.  
  196.    public synchronized boolean isConsumer(ImageConsumer ic) {
  197.       for(ImageDecoder id = this.decoders; id != null; id = id.next) {
  198.          if (id.isConsumer(ic)) {
  199.             return true;
  200.          }
  201.       }
  202.  
  203.       return ImageConsumerQueue.isConsumer(this.consumers, ic);
  204.    }
  205.  
  206.    void latchConsumers(ImageDecoder id) {
  207.       ImageConsumerQueue cq;
  208.       synchronized(this) {
  209.          if (id != this.decoder) {
  210.             return;
  211.          }
  212.  
  213.          cq = this.consumers;
  214.       }
  215.  
  216.       while(cq != null) {
  217.          synchronized(this) {
  218.             if (!cq.interested) {
  219.                cq = cq.next;
  220.                continue;
  221.             }
  222.          }
  223.  
  224.          if (!id.catchupConsumer(this, cq.consumer)) {
  225.             synchronized(this) {
  226.                this.doneDecoding(id);
  227.                return;
  228.             }
  229.          }
  230.  
  231.          synchronized(this){}
  232.  
  233.          try {
  234.             ImageConsumerQueue cqn = cq.next;
  235.             if (cq.interested) {
  236.                this.consumers = ImageConsumerQueue.removeConsumer(this.consumers, cq.consumer, true);
  237.                cq.next = id.queue;
  238.                id.queue = cq;
  239.             }
  240.  
  241.             cq = cqn;
  242.          } catch (Throwable var11) {
  243.             throw var11;
  244.          }
  245.       }
  246.  
  247.    }
  248.  
  249.    synchronized void printQueue(ImageConsumerQueue cq, String prefix) {
  250.       while(cq != null) {
  251.          System.out.println(prefix + cq);
  252.          cq = cq.next;
  253.       }
  254.  
  255.    }
  256.  
  257.    synchronized void printQueues(String title) {
  258.       System.out.println(title + "[ -----------");
  259.       this.printQueue(this.consumers, "  ");
  260.  
  261.       for(ImageDecoder id = this.decoders; id != null; id = id.next) {
  262.          System.out.println("    " + id);
  263.          this.printQueue(id.queue, "      ");
  264.       }
  265.  
  266.       System.out.println("----------- ]" + title);
  267.    }
  268.  
  269.    public synchronized void removeConsumer(ImageConsumer ic) {
  270.       for(ImageDecoder id = this.decoders; id != null; id = id.next) {
  271.          id.removeConsumer(ic);
  272.       }
  273.  
  274.       this.consumers = ImageConsumerQueue.removeConsumer(this.consumers, ic, false);
  275.       if (this.consumers == null) {
  276.          this.stopProduction();
  277.       }
  278.  
  279.    }
  280.  
  281.    private synchronized void removeDecoder(ImageDecoder mydecoder) {
  282.       this.doneDecoding(mydecoder);
  283.       ImageDecoder idprev = null;
  284.  
  285.       for(ImageDecoder id = this.decoders; id != null; id = id.next) {
  286.          if (id == mydecoder) {
  287.             if (idprev == null) {
  288.                this.decoders = id.next;
  289.             } else {
  290.                idprev.next = id.next;
  291.             }
  292.             break;
  293.          }
  294.  
  295.          idprev = id;
  296.       }
  297.  
  298.    }
  299.  
  300.    public void requestTopDownLeftRightResend(ImageConsumer ic) {
  301.       for(ImageDecoder id = this.decoders; id != null; id = id.next) {
  302.          if (id.isConsumer(ic)) {
  303.             id.replayConsumer(ic);
  304.          }
  305.       }
  306.  
  307.    }
  308.  
  309.    private void setDecoder(ImageDecoder mydecoder) {
  310.       synchronized(this){}
  311.  
  312.       ImageConsumerQueue cq;
  313.       try {
  314.          mydecoder.next = this.decoders;
  315.          this.decoders = mydecoder;
  316.          this.decoder = mydecoder;
  317.          cq = this.consumers;
  318.          mydecoder.queue = cq;
  319.          this.consumers = null;
  320.          this.awaitingFetch = false;
  321.       } catch (Throwable var5) {
  322.          throw var5;
  323.       }
  324.  
  325.       for(; cq != null; cq = cq.next) {
  326.          if (cq.interested && !this.checkSecurity(cq.securityContext, true)) {
  327.             this.errorConsumer(cq);
  328.          }
  329.       }
  330.  
  331.    }
  332.  
  333.    synchronized void setPixelStore(ImageDecoder id, PixelStore storage) {
  334.       if (id == this.decoder) {
  335.          this.pixelstore = storage;
  336.       }
  337.  
  338.    }
  339.  
  340.    private synchronized void startProduction() {
  341.       if (!this.awaitingFetch) {
  342.          ImageFetcher.add(this);
  343.          this.awaitingFetch = true;
  344.       }
  345.  
  346.    }
  347.  
  348.    public void startProduction(ImageConsumer ic) {
  349.       this.addConsumer(ic, true);
  350.    }
  351.  
  352.    private synchronized void stopProduction() {
  353.       if (this.awaitingFetch && ImageFetcher.remove(this)) {
  354.          this.awaitingFetch = false;
  355.       }
  356.  
  357.    }
  358.  
  359.    private boolean updateFromStore() {
  360.       PixelStore store;
  361.       ImageConsumerQueue cq;
  362.       synchronized(this) {
  363.          store = this.pixelstore;
  364.          if (store == null) {
  365.             return false;
  366.          }
  367.  
  368.          cq = this.consumers;
  369.       }
  370.  
  371.       while(cq != null) {
  372.          boolean secure;
  373.          synchronized(this) {
  374.             if (!cq.interested) {
  375.                cq = cq.next;
  376.                continue;
  377.             }
  378.  
  379.             secure = this.checkSecurity(cq.securityContext, true);
  380.          }
  381.  
  382.          if (secure && !store.replay(this, cq.consumer)) {
  383.             return false;
  384.          }
  385.  
  386.          synchronized(this){}
  387.  
  388.          try {
  389.             if (cq.interested) {
  390.                this.errorConsumer(cq);
  391.             }
  392.  
  393.             cq = cq.next;
  394.          } catch (Throwable var8) {
  395.             throw var8;
  396.          }
  397.       }
  398.  
  399.       return true;
  400.    }
  401. }
  402.